#include "StdAfx.h"
#include "handler_test.h"
#include <handler\rtt_handler.h>
#include <handler\user_handler.h>

 
namespace fly {

	namespace jld{
		using namespace server;

		namespace test {

			handler_test::handler_test()
			{
			}
			void handler_test::SetUp()
			{
				handler_ = (new user_handler())->init_branch();
				handler_->set_next(new rtt_handler());
			}


			void handler_test::encode_header(google::protobuf::Message& msg,const PID pid)
			{
				header_.set_pid(pid);
				header_.set_size(msg.ByteSize());
				ostream os(&recv_buf_.buf());
				header_.SerializeToOstream(&os);
				os.clear();
			}

			void handler_test::encode_body(google::protobuf::Message& msg)
			{
				ostream os(&recv_buf_.buf());
				msg.SerializeToOstream(&os);
				os.clear();
			}
			bool handler_test::handle_header()
			{
				return recv_buf_.handle_header();
			}
			bool handler_test::handle_body()
			{
				return handler_->handle_recv(recv_buf_, send_buf_, conn_info_);
			}
			void handler_test::decode_header()
			{
				header_.Clear();
				std::string str_header(asio::buffers_begin(*(send_buf_.packets().front().buf)),
					asio::buffers_begin(*(send_buf_.packets().front().buf)) + send_buf_.header_size());
				header_.ParseFromString(str_header);
			}

			void handler_test::decode_body(google::protobuf::Message& msg)
			{
				std::string str_body(asio::buffers_begin(*(send_buf_.packets().front().buf)) + send_buf_.header_size()
					,asio::buffers_end(*(send_buf_.packets().front().buf)));
				msg.ParseFromString(str_body);
				send_buf_.packets().pop();
			}
			void handler_test::TearDown()
			{
				delete handler_;
			}
		}
	}
}
